#

| Chapter 33 Batch Editing |

## 33.4 Useful ex Commands

Many line editor commands are not particularly useful in scripts. The two commands that you will use far and away the most often are s (substitute), to replace one pattern with another, and d (delete) to delete one or more lines. On occasion, though, you'll want to insert text from a script. (Editing scripts built by diff (28.9) make heavy use of insert, append, delete, and change commands.) And of course, you need commands to write and quit the editor.

Here's the syntax of most of the commands you may encounter in ex editing scripts. (The ed editor understands the abbreviated versions of some, but not all of, these commands.) Elements in [brackets] are optional; don't type the `[` or `]`. (The leading colon is the ex command character used to issue an ex command from vi; in a script, the colon would be omitted.)

append

  [address] a[!] text `\.` Append text at specified address, or at present address if none is specified. Add a ! to switch the autoindent setting that will be used during input. For example, if autoindent was enabled, ! disables it.

change

  [address] c[!] text `\.` Replace the specified lines with text. Add a ! to switch the autoindent setting during input of text.

copy

  [address] co destination [address] t destination Copy the lines included in address to the specified destination address.

  >     :1,10 co 50
  >     :1,10t50

delete

  [address] d [buffer] Delete the lines included in address. If buffer is specified, save or append the text to the named buffer.

  >     :/Part I/,/Part II/-1d   Delete to line above "Part II"
  >     :/main/+d   Delete line below "main"
  >     :.,$/d   Delete from this line to last line

global

  [address] g[!]/pattern/[commands] Execute commands on all lines that contain pattern or, if address is specified, on all lines within that range. If commands are not specified, print all such lines. (Exception: doesn't print when you use it from vi by typing `:` first. You'll need to add a p, as in the second example below). If ! is used, execute commands on all lines that don't contain pattern.

  >     :g/Unix/
  >     :g/Unix/p
  >     :g/Name:/s/tom/Tom/

insert

  [address] i[!] text `\.` Insert text at line before the specified address, or at present address if none is specified. Add a ! to switch the autoindent setting during input of text.

move

  [address] m destination Move the lines specified by address to the destination address.

  >     :.,/Note/m /END/          Move block after line containing "END"

print

  [address] p [count] Print the lines specified by address. count specifies the number of lines to print, starting with address.

  >     :100;+5p                  Show line 100 and the next five lines

quit

  q[!] Terminate current editing session. Use ! to discard changes made since the last save. If the editing session includes additional files in the argument list that were never accessed, quit by typing q! or by typing q twice.

read

  [address] r file Copy in the text from file on the line below the specified address. If file is not specified, the current filename is used.

  >     :0r $HOME/data   Read file in at top of current file

read

  [address] r !command Read the output of UNIX command into the text after the line specified by address.

      cal
      :$r !cal   Place a calendar at end of file

source

  so file Read and execute ex commands from file.

  >     :so $HOME/.exrc

substitute

  [address] s [/pattern/replacement/] [options] [count] Replace first instance of pattern on each of the specified lines with replacement. If pattern and replacement are omitted, repeat last substitution. count specifies the number of lines on which to substitute, starting with address. Options

c

  Prompt for confirmation before each change.

g

  Substitute all instances of pattern on each line.

p

  Print the last line on which a substitution was made.

      c
      \U
      :1,10s/yes/no/g      Substitute on first 10 lines
      :%s/[Hh]ello/Hi/gc   Confirm global substitutions
      :s/Fortran/\U&/ 3    Uppercase "Fortran" on next 3 lines

write

  [address] w[!] [[>>] file] Write lines specified by address to file, or write full contents of buffer if address is not specified. If file is also omitted, save the contents of the buffer to the current filename. If >> file is used, write contents to the end of an existing file. The ! flag forces the editor to write over any current contents of file.

  >     :1,10w name_list   Copy first 10 lines to name_list
  >     :50w >> name_list   Now append line 50

write

  [address] w !command Write lines specified by address, or write full contents of buffer if address is not specified, to the standard input (13.1) of command.

  >     :1,10w !spell   Send first 10 lines to the spell command
  >     :w !lpr   Print entire buffer with lpr command

- TOR, DG

33.3 Line Addressing |  | 33.5 Running Editing Scripts Within vi